package grap;

import java.awt.Color;
import data.*;
import ui.GameMap;
import util.LineBwtTwoPoint;
import util.ReadMap;
import util.WriteMap;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Calendar;

import javax.swing.JOptionPane;

/**
 * @author lazyman
 * 绘制地图的面板
 */
public class CreateMap{
	private GameMap gameMap;
	
	private Point start;
	private Point end;
	
	private boolean markMouse = false;
	
	public static void main(String[] args) {
		CreateMap creatMap = new CreateMap();
		creatMap.initListen();
	}
	
	public CreateMap() {
		gameMap = new GameMap(20,20);
	}
	
	public void initListen() {
		
		initButton();
		initLabel();
	}
	
	public void initButton() {
		
		gameMap.btnOk.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String rowStr = gameMap.rowsText.getText();
				String colStr = gameMap.colsText.getText();
				
				if(rowStr == null || rowStr.length() == 0 || colStr == null || colStr.length() == 0) {
					return;
				}
				updateMap(Integer.parseInt(rowStr), Integer.parseInt(colStr));
			}
		});
		
		gameMap.btnSave.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String fileName =  gameMap.fileNameText.getText();				
				if(fileName == null || fileName.length() == 0) {
					Calendar cal = Calendar.getInstance();
					fileName = cal.get(Calendar.MONTH) + 1 + "-" + cal.get(Calendar.DATE) +"-"+ cal.get(Calendar.MINUTE)+"-"+ cal.get(Calendar.SECOND);
				}
				WriteMap out = new WriteMap(fileName, gameMap.grapData);
				out.write();
				JOptionPane.showMessageDialog(null, "success", "Message", JOptionPane.INFORMATION_MESSAGE); 
			}
		});
		
		gameMap.btnRead.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String fileName =  gameMap.fileNameText.getText();				
				if(fileName == null || fileName.length() == 0) {
					return;
				}
				ReadMap  read = new ReadMap(fileName);
				read.read();
				updateMap(read.getMatrix());
			}
		});
	}
	
	public void initLabel() {
		for(Label[] labels : gameMap.labels) {
			for(Label label : labels) {
				label.addMouseListener(new MouseAdapter() {
					
					public void mouseClicked(MouseEvent e) {
						Label label = (Label) e.getSource();
						
						if(label.getBackground() == Color.black) {
							label.setBackground(new Color(238,238,238));
							gameMap.grapData.getGrap()[label.y][label.x] = 0;
						}else {
							label.setBackground(Color.black);
							gameMap.grapData.getGrap()[label.y][label.x] = 1;
						}
					}
					
					public void mouseEntered(MouseEvent e) {
						if(e.isControlDown()) {
							Label label = (Label) e.getSource();
							label.setBackground(Color.black);
							gameMap.grapData.getGrap()[label.y][label.x] = 1;
						}
					}
				});
			}
		}
	}
	
	//更新地图的行数和列数
	private void updateMap(int rows,int cols) {
		gameMap.updateGrap(rows, cols);
		initLabel();
	}
	
	private void updateMap(int[][] matrix) {
		gameMap.updateGrap(matrix.length, matrix[0].length);
		gameMap.updateLabel(matrix);
		initLabel();
	}
	
	
	//获取鼠标单击的点
	public void getClickPoint(Label p) {
		if(markMouse) {
			end = new Point(p.x,p.y);
			//绘制start到end之间的所有点
			drawLine();
		}else{
			start = new Point(p.x,p.y);
		}
		markMouse = !markMouse;	
	}
	
	public void drawLine() {
		if(start == null || end == null) {
			return;
		}
		LineBwtTwoPoint path = new LineBwtTwoPoint(start, end);
		path.GetPath();
		path.list.add(start);

		for(Point p : path.list) {
			gameMap.setLabelColor(p, Color.black);
			gameMap.grapData.getGrap()[p.getY()][p.getX()] = 1;
		}
	}
}
